package com.ccf.business.etl.controller;

import cn.hutool.core.util.StrUtil;
import com.ccf.business.etl.model.Node;
import com.ccf.business.etl.service.INodeService;
import com.common.model.pojo.Query;
import com.common.model.pojo.ResultVO;
import com.common.model.result.PageResult;
import com.common.model.result.Result;
import com.service.database.model.DataModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * 节点表
 *
 * @author ccf
 * @date 2021-11-29 18:02:10
 */
@Slf4j
@RestController
@RequestMapping("/node")
@Api(tags = "节点表")
public class NodeController {
    @Autowired
    private INodeService nodeService;

    /**
     * 列表
     */
    @ApiOperation(value = "分页查询列表")
    @GetMapping
    public PageResult list(Query query) {
        return nodeService.findList(query);
    }


    /**
     * 查询
     */
    @ApiOperation(value = "查询")
    @GetMapping("/{id}")
    public Result getById(@PathVariable String id) {
        Node model = nodeService.getById(id);
        return Result.succeed(model, "查询成功");
    }

    /**
     * 新增or更新
     */
    @ApiOperation(value = "保存")
    @PostMapping
    public Result save(@RequestBody Node node) {
        nodeService.saveOrUpdate(node);
        return Result.succeed("保存成功");
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable String id) {
        //批量删除
        if (StrUtil.contains(id, ",")){
            String[] ids = StrUtil.split(id, ",");
            nodeService.removeByIds(Arrays.stream(ids).collect(Collectors.toList()));
        } else {
            nodeService.removeById(id);
        }
        return Result.succeed("删除成功");
    }

    /**
     * 预览图片
     *  访问方式
     * @param
     * @return
     */
    @ApiOperation(value = "表格预览",notes = "仅仅支持csv,xls,xlsx")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "文件路径"),
            @ApiImplicitParam(name = "header", value = "表头设置"),
            @ApiImplicitParam(name = "encoding", value = "编码格式"),
    })
    @GetMapping(value = "/viewTable")
    public Result viewTable(String file, String header, String encoding)  {
        try {
            DataModel dataModel = nodeService.viewTable(file, header, encoding);
            return Result.succeed(dataModel, "查询成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed("查询失败");
        }
    }

    /**
     * 预览结果
     *  访问方式
     * @param
     * @return
     */
    @ApiOperation(value = "预览结果",notes = "传递节点id")
    @ApiImplicitParam(name = "nodeId", value = "节点id")
    @GetMapping(value = "/viewResult/{nodeId}")
    public Result viewResult(@PathVariable String nodeId)  {
        ResultVO vo = null;
        try {
            vo = nodeService.viewResult(nodeId);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed("查询失败");
        }
        return Result.succeed(vo, "查询成功");
    }
}
